package org.gwu.seuss.persistence;

import java.util.List;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.gwu.seuss.domain.User;

public interface UsersMapper {

	@Select("SELECT * FROM users")
	public List<User> selectAllUsers();

	@Select("SELECT * FROM users WHERE email = #{email}")
	public User selectUserByEmail(@Param("email") String email);

	@Update("UPDATE users SET sessionKey = #{sessionKey} WHERE email = #{email}")
	public int updateUsersSessionKey(@Param("sessionKey") byte[] sessionKey, @Param("email") String email);

	@Update("UPDATE users SET passwordHash = #{passwordHash}, salt = #{salt} WHERE email = #{email}")
	public int updateUserPassword(@Param("email") String email, @Param("passwordHash") String passwordHash, @Param("salt") String salt);

	@Update("UPDATE users SET email = #{newEmail} WHERE email = #{oldEmail}")
	public int updateUserEmail(@Param("oldEmail") String oldEmail, @Param("newEmail") String newEmail);

	@Insert("INSERT INTO users (email, passwordhash, salt, directory) VALUES (#{email}, #{passwordHash}, #{salt}, #{directory})")
	@Options(useGeneratedKeys = true, keyProperty = "userId", keyColumn = "userId")
	public int insertUser(User user);

	@Update("UPDATE users SET disabled = true WHERE email = #{email}")
	public int disableUser(@Param("email") String email);

	@Update("UPDATE users SET disabled = false WHERE email = #{email}")
	public int enableUser(@Param("email") String email);

	@Update("Delete FROM users WHERE email = #{email}")
	public int removeUser(@Param("email") String email);

}